package view.plugin
{
	//休息室
	
	import event.GlobalEvent;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import flash3.framework.Singleton;
	
	import global.Resource;
	
	import model.*;
	
	import util.EffectCharAdd;
	import util.ImageNumber;
	
	import vo.GameServer;
	import vo.MoneyPoolItem;
	import vo.ServerRunInfo;
	import vo.UserScore;
	
	public class WaitPanel extends PanelBase
	{
		public var btnContinue:SimpleButton;
		public var btnExit:SimpleButton;
		public var petrol:TextField;
		public var gold:TextField;
		public var score:TextField;
		public var rank:TextField;
		public var bonusPoolCur:TextField;
		public var bonusPoolA:TextField;
		public var bonusPoolB:TextField;
		public var bonusPoolC:TextField;
		public var item:Array;
		private var dlg:ExitPanel = null;
		public var awardTimer:EffectCharAdd;
		public var waitTimer:EffectCharAdd;
		private var sleepTime:ImageNumber = null;
		private var bonusTime:ImageNumber = null;
		private var updateTimer:Timer = null;
		
		public static var CONTINUE_GAME:String = "continue_game";
		public static var WAIT_COMPLETE:String = "wait_complete";
		public static var AWARD_COMPLETE:String = "award_complete";
		public static const EXIT:String = "exit";
		
		public static const WAIT_TIME:Number = 600;
		
		public function WaitPanel()
		{
			super();
			var main:MovieClip = Resource.i.getMovieClip("dlgWait", "DlgWait");
			addChild(main);
			btnContinue = main.btnContinue;
			btnExit = main.btnExit;
			petrol	= main.petrol;
			gold	= main.gold;
			score	= main.score;
			rank	= main.rank;
			
			sleepTime = new ImageNumber(Resource.i.getBitmapData("number03"));
			bonusTime = new ImageNumber(Resource.i.getBitmapData("number03"));
			sleepTime.x = 310; sleepTime.y = 25;
			sleepTime.scaleX = 0.8; sleepTime.scaleY = 0.8;
			bonusTime.x = 366; bonusTime.y = 218;
			bonusTime.scaleX = 0.6; bonusTime.scaleY = 0.6;
			addChild(sleepTime);
			addChild(bonusTime);
			
			bonusPoolCur	= main.bonusPoolCur;
//			bonusTime	= main.bonusTime;
			item = [main.item1,main.item2,main.item3];
			btnContinue.addEventListener(MouseEvent.CLICK, onClickBtnContinue);
			btnExit.addEventListener(MouseEvent.CLICK, onClickBtnExit);
			
			awardTimer = new EffectCharAdd(null, 1000, 1, 0, 0, false);
			waitTimer = new EffectCharAdd(null, 1000, 1, WAIT_TIME, 0, false);
			
			updateTimer = new Timer(30000);
		}
		
		private function onClickBtnContinue(evt:MouseEvent):void
		{
			show(false);
			showDlg(false);
			this.dispatchEvent(new Event(CONTINUE_GAME));
		}
		
		private function onClickBtnExit(evt:MouseEvent):void
		{
			showDlg(true);
		}
		
		private function showDlg(v:Boolean):void
		{
			this.mouseChildren = !v;
			dlg = ExitPanel.i;
			if(v)
			{
				dlg.text = ExitPanel.getStr1();
				dlg.addEventListener(ExitPanel.OK, onClickDlgBtnOK);
				dlg.addEventListener(ExitPanel.CANSEL, onClickDlgBtnCansel);
				var sprite:Sprite = ChessLobby.dialog;
				sprite.addChild(dlg);
			}
			else
			{
				if(dlg.parent)
					dlg.parent.removeChild(dlg);
				dlg.removeEventListener(ExitPanel.OK, onClickDlgBtnOK);
				dlg.removeEventListener(ExitPanel.CANSEL, onClickDlgBtnCansel);
			}
		}
		private function onClickDlgBtnOK(evt:Event):void
		{
			showDlg(false);
			this.show(false);
			this.dispatchEvent(new Event(EXIT));
		}
		private function onClickDlgBtnCansel(evt:Event):void
		{
			showDlg(false);
		}
		override public function show(v:Boolean):void
		{
			if(v)this.mouseChildren = true;
			if(!v)showDlg(false);
			super.show(v);
		}
		
		public function updateData():void
		{
			var serverRunInfo:ServerRunInfo = GameModel.i.serverRunInfo;
			var prizePool:MoneyPoolItem = serverRunInfo.prizesPool;
			var poolItem:MoneyPoolItem = GameModel.i.poolItem;
			var userScore:UserScore = MeInfoModel.i.userScore;
			petrol.text = userScore.lScore.toString();
			gold.text	= "(可兑回 " + userScore.lMoneyGold.toString() + "金币)";
			score.text	= serverRunInfo.rankInfo.score.toString();
			rank.text	= serverRunInfo.rankInfo.rank==0 ? "未排名" : (serverRunInfo.rankInfo.rank.toString()+"/"+serverRunInfo.rankInfo.rankCount.toString());
			for(var i:uint = 0; i < item.length; i++)
			{
				if(item[i] && prizePool.listPoolItem[i])
				{
					item[i].visible = true;
					item[i].gold.text = prizePool.listPoolItem[i].money.toString();
					item[i].awardName.text = poolItem.listPoolItem[i].name + "奖池";
					item[i].icon.gotoAndStop(2);
				}
				else
				{
					item[i].visible = false;
				}
			}
		}
		public function start():void
		{
			var serverRunInfo:ServerRunInfo = GameModel.i.serverRunInfo;
			sleepTime.value = getTime(WAIT_TIME);
			bonusTime.value = getTime(serverRunInfo.eventleftime);
			awardTimer.setRangle(serverRunInfo.eventleftime, 0);
			awardTimer.addEventListener(Event.COMPLETE, onAwardTimerComplete);
			awardTimer.addEventListener(EffectCharAdd.TIMER_COUNT, onAwardTimerCount);
			waitTimer.addEventListener(Event.COMPLETE, onWaitTimerComplete);
			waitTimer.addEventListener(EffectCharAdd.TIMER_COUNT, onWaitTimerCount);
			awardTimer.start();
			waitTimer.start();
		}
		override public function closeHandle():void
		{
			awardTimer.stop();
			waitTimer.stop();
			updateTimer.stop();
			awardTimer.removeEventListener(Event.COMPLETE, onAwardTimerComplete);
			awardTimer.removeEventListener(EffectCharAdd.TIMER_COUNT, onAwardTimerCount);
			waitTimer.removeEventListener(Event.COMPLETE, onWaitTimerComplete);
			waitTimer.removeEventListener(EffectCharAdd.TIMER_COUNT, onWaitTimerCount);
			updateTimer.removeEventListener(TimerEvent.TIMER, onUpdateData);
		}
		override public function showHandle():void
		{
			start();
			updateTimer.addEventListener(TimerEvent.TIMER, onUpdateData);
			updateTimer.reset();
			updateTimer.start();
		}
		private function onUpdateData(evt:TimerEvent):void
		{
			trace("更新休息数据");
			this.updateData();
		}
		private function onAwardTimerComplete(evt:Event):void
		{
			awardTimer.stop();
			awardTimer.removeEventListener(EffectCharAdd.TIMER_COUNT, onAwardTimerCount);
			awardTimer.removeEventListener(Event.COMPLETE, onAwardTimerComplete);
			this.dispatchEvent(new Event(AWARD_COMPLETE));
		}
		private function onAwardTimerCount(evt:GlobalEvent):void
		{
			bonusTime.value = getTime(evt.data);
		}
		private function onWaitTimerComplete(evt:Event):void
		{
			waitTimer.removeEventListener(EffectCharAdd.TIMER_COUNT, onWaitTimerCount);
			waitTimer.removeEventListener(Event.COMPLETE, onWaitTimerComplete);
			this.dispatchEvent(new Event(WAIT_COMPLETE));
		}
		private function onWaitTimerCount(evt:GlobalEvent):void
		{
			sleepTime.value = getTime(evt.data);
		}
		private function getTime(time:uint):String
		{
			var count1:uint = time / 60;
			var count2:uint = time % 60;
			var str1:String = count1.toString();
			var str2:String = count2.toString();
			if(str1.length <= 1)str1 = "0" + str1;
			if(str2.length <= 1)str2 = "0" + str2;
			return str1 + ":" + str2;
		}
		static public function get i():WaitPanel
		{
			return Singleton.getInstanceOrCreate(WaitPanel) as WaitPanel;
		}
	}
}